package Socket_Chat;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketClient4 {
	
	private static ExecutorService exec = Executors.newCachedThreadPool(); 
	
	public static void main(String[] args) throws Exception {  
		new SocketClient4();
	}
	
	public SocketClient4() {
		try {
			Socket socket = new Socket("127.0.0.1", 7777);  
			exec.execute(new Sender(socket));
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String msg;
			while((msg = br.readLine()) != null) { 
				System.out.println(msg);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	class Sender implements Runnable {
		private Socket socket; 
		public Sender(Socket socket) {  
			this.socket = socket;
		}
		
		@Override
		public void run() {
			try {
				String msg = ""; 
				OutputStream out = null;
				while(true) {
					String userInput = new Scanner(System.in).nextLine(); 
//					new DataOutputStream(socket.getOutputStream()).write(msg.getBytes());
					out = socket.getOutputStream();
				
					
					
					out.write(msg.getBytes());
					out.flush();
				}
			
			
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}  
		
		
		
		
	}
	
	
}
